package ui.fragment.homepagefragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import adapter.ExerciseBookAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.ExerciseBookBean;
import student.andlei.com.student.R;
import ui.activity.GoodOrErrorQuestionActivity;
import utils.GsonUtils;

/**
 * 习题本
 */
public class ExerciseBookFragment extends BaseNetFragment {
    private RadioGroup radioGroup;
    private RadioButton rb_good,
            rb_matter;
    private ExerciseBookAdapter adapter;
    private RecyclerView recyclerView;
    private int type = 1;
    private List<ExerciseBookBean> work_list = new ArrayList();

    public static Fragment getInstance() {
        return new ExerciseBookFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        if (baseBean.code == 200) {
            work_list = GsonUtils.getInstance(context).parseNoHeaderJArray(baseBean, ExerciseBookBean.class);
            for (ExerciseBookBean bookBean : work_list) {
                bookBean.type = type;
            }
            adapter.setNewData(work_list);
        }
    }

    @Override
    public void error(String action, Throwable e) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_exercisebook;
    }

    @Override
    protected void findViews() {
        radioGroup = getView(R.id.rd_group);
        rb_good = getView(R.id.rb_all);
        rb_matter = getView(R.id.rb_timetable);
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        recyclerView.setLayoutManager(new GridLayoutManager(this.getActivity(), 4));
        adapter = new ExerciseBookAdapter(context, R.layout.item_work, work_list);
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEmptyView(R.layout.empty_view);
        ((TextView)(adapter.getEmptyView().findViewById(R.id.tv_empty))).setText("你还没有记录哦");
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    //好题本
                    case R.id.rb_all:
                      //  toast("好题本");
                        type = 1;
                        loadData();
                        break;
                    //错题本
                    case R.id.rb_timetable:
                      //  toast("错题本");
                        type = 2;
                        loadData();
                        break;
                }
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("subject_name",work_list.get(position).subject_name);
                bundle.putInt("subject_id", work_list.get(position).subject_id);
                bundle.putInt("type",type);
                jumpTo(GoodOrErrorQuestionActivity.class, bundle);
            }
        });
    }

    @Override
    protected void formatData() {
//        for (int i=1;i<=10;i++){
//            WorkBean bean = new WorkBean();
//            bean.setFinish("已完成："+i+"份");
//            bean.setImgurl("88");
//            bean.setSubject("语文");
//            work_list.add(bean);
//        }
        loadData();
    }

    private void loadData() {
        Map<String, String> map = new HashMap<>();
        //1:好题（默认） 2:错题
        map.put("type", "" + type);
        post("Book/subjectList", map, BaseBean.class, false);
    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {

    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            loadData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }
}
